using Newtonsoft.Json;
using LiJ.Common.Core;
using LiJ.Http.Mos;
using LiJ.Framework.Official.Card.Mos;
using LiJ.Framework.Official.SysTools.Mos;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace LiJ.Framework.Official.Card
{
	/// <summary>
	///   卡券接口
	/// </summary>
	/// <summary>
	///  卡券接口统计部分
	/// </summary>
	public class WxOffCardApi : WxOffBaseApi
	{
		/// <summary>
		///   查询cardcode 核销状态
		/// </summary>
		/// <param name="code">单张卡券的唯一标准</param>
		/// <param name="cardId">卡券ID代表一类卡券。自定义code卡券必填</param>
		/// <param name="checkConsume">是否校验code核销状态，填入true和false时的code异常状态返回数据不同</param>
		/// <returns></returns>
		public async Task<WxGetCardCodeConsumeResp> GetCardCodeConsumeState(string code, string cardId = "", bool checkConsume = false)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/code/get";
			LHttpRequest.CustomBody = string.Format("{{\"code\":\"{0}\",\"card_id\":\"{1}\",\"check_consume\":{2}}}", code, cardId, checkConsume);
			return await base.RestCommonOffcialAsync<WxGetCardCodeConsumeResp>(LHttpRequest, null);
		}

		/// <summary>
		///  解密链接中的code
		/// </summary>
		/// <param name="encryptCode"></param>
		/// <returns></returns>
		public async Task<WxCardCodeDecryptResp> DecryptCodeAsync(string encryptCode)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/code/decrypt";
			LHttpRequest.CustomBody = string.Format("{{\"encrypt_code\":\"{0}\"}}", encryptCode);
			return await base.RestCommonOffcialAsync<WxCardCodeDecryptResp>(LHttpRequest, null);
		}

		/// <summary>
		///  核销卡券
		/// </summary>
		/// <param name="code">需核销的Code码</param>
		/// <param name="cardId">卡券ID。创建卡券时use_custom_code填写true时必填。非自定义Code不必填写</param>
		/// <returns></returns>
		public async Task<WxCardConsumeResp> ConsumeCardCodeAsync(string code, string cardId = "")
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/code/consume";
			LHttpRequest.CustomBody = string.Format("{{\"code\":\"{0}\",\"card_id\":\"{1}\"}}", code, cardId);
			return await base.RestCommonOffcialAsync<WxCardConsumeResp>(LHttpRequest, null);
		}

		/// <summary>
		///   生成单卡券投放二维码
		/// </summary>
		/// <param name="type"></param>
		/// <param name="expireSeconds"></param>
		/// <param name="cardQrMo"></param>
		/// <returns></returns>
		public async Task<WxCardQrCodeResp> CreateCardQrCodeAsync(WxQrCodeType type, int expireSeconds, WxCardQrMo cardQrMo)
		{
			WxCreateCardQrReq actionInfo = new WxCreateCardQrReq
			{
				expire_seconds = expireSeconds,
				action_name = type.ToString(),
				action_info = new
				{
					card = cardQrMo
				}
			};
			return await this.CreateCardQrCodeAsync(actionInfo);
		}

		/// <summary>
		///   生成多卡券投放二维码
		/// </summary>
		/// <param name="type"></param>
		/// <param name="expireSeconds"></param>
		/// <param name="cardList"></param>
		/// <returns></returns>
		public async Task<WxCardQrCodeResp> CreateMultiCardQrCode(WxQrCodeType type, int expireSeconds, List<WxCardQrMo> cardList)
		{
			if (cardList != null && cardList.Count <= 5)
			{
				WxCreateCardQrReq actionInfo = new WxCreateCardQrReq
				{
					expire_seconds = expireSeconds,
					action_name = type.ToString(),
					action_info = new
					{
						multiple_card = new
						{
							card_list = cardList
						}
					}
				};
				return await this.CreateCardQrCodeAsync(actionInfo);
			}
			return new WxCardQrCodeResp
			{
				Ret = 1301,
				Msg = "卡券数目不和要求，请不要为空或超过五个！"
			};
		}

		/// <summary>
		/// 生成卡券投放二维码
		/// </summary>
		/// <param name="actionInfo"></param>
		/// <returns></returns>
		private async Task<WxCardQrCodeResp> CreateCardQrCodeAsync(WxCreateCardQrReq actionInfo)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/qrcode/create";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(actionInfo);
			return await base.RestCommonOffcialAsync<WxCardQrCodeResp>(LHttpRequest, null);
		}

		/// <summary>
		///   导入卡券code
		/// </summary>
		/// <param name="cardId">需要进行导入code的卡券ID</param>
		/// <param name="codes">需导入微信卡券后台的自定义code，上限为100个</param>
		/// <returns></returns>
		public async Task<WxImportCardCodeResp> ImportCardCodeAsync(string cardId, List<string> codes)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/code/deposit";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				card_id = cardId,
				code = codes
			});
			return await base.RestCommonOffcialAsync<WxImportCardCodeResp>(LHttpRequest, null);
		}

		/// <summary>
		///   查询导入code数目接口
		/// </summary>
		/// <param name="cardId"></param>
		/// <returns></returns>
		public async Task<WxGetImportCodeCountResp> GetImportCodeCountAsync(string cardId)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/code/getdepositcount";
			LHttpRequest.CustomBody = string.Format("{{\"card_id\":\"{0}\"}}", cardId);
			return await base.RestCommonOffcialAsync<WxGetImportCodeCountResp>(LHttpRequest, null);
		}

		/// <summary>
		///   验证已经导入的code信息
		/// </summary>
		/// <param name="cardId">需要进行导入code的卡券ID</param>
		/// <param name="codes">需导入微信卡券后台的自定义code，上限为100个</param>
		/// <returns></returns>
		public async Task<WxCheckImportCodeResp> CheckImportCodeAsync(string cardId, List<string> codes)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/code/getdepositcount";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				card_id = cardId,
				code = codes
			});
			return await base.RestCommonOffcialAsync<WxCheckImportCodeResp>(LHttpRequest, null);
		}

		/// <summary>
		///   获取图文推送的卡券信息
		/// </summary>
		/// <param name="cardId"></param>
		/// <returns></returns>
		public async Task<WxGetCardArticleContentResp> GetArticleContentAsync(string cardId)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/mpnews/gethtml";
			LHttpRequest.CustomBody = string.Format("{{\"card_id\":\"{0}\"}}", cardId);
			return await base.RestCommonOffcialAsync<WxGetCardArticleContentResp>(LHttpRequest, null);
		}

		/// <summary>
		///  创建卡券投放货架接口
		/// </summary>
		/// <param name="pageReq"></param>
		/// <returns></returns>
		public async Task<WxCreateCardLandPageResp> CreateLandPageAsync(WxCreateCardLandPageReq pageReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/landingpage/create";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(pageReq);
			return await base.RestCommonOffcialAsync<WxCreateCardLandPageResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 设置卡券测试白名单
		/// </summary>
		/// <param name="openIds"> 可选 openid列表 </param>
		/// <param name="names">可选  微信号列表  二者必填其一</param>
		/// <returns></returns>
		public async Task<WxBaseResp> SetTestWhiteListAsync(List<string> openIds, List<string> names)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/testwhitelist/set";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				openid = openIds,
				username = names
			});
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		///  构造函数
		/// </summary>
		/// <param name="config">配置信息，如果这里不传，需要在程序入口静态 WxBaseApi.DefaultConfig 属性赋值</param>
		public WxOffCardApi(AppConfig config = null)
			: base(config)
		{
		}

		/// <summary>
		///   添加现金卡券接口
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		public async Task<WxAddCardResp> AddCashCardAsync(WxCashCardMo cardReq)
		{
			var cardReq2 = new
			{
				card_type = WxCardType.CASH,
				cash = cardReq
			};
			return await this.AddCardAsync(cardReq2);
		}

		/// <summary>
		///   添加优惠卡券接口
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		public async Task<WxAddCardResp> AddCoupnCardAsync(WxCouponCardMo cardReq)
		{
			var cardReq2 = new
			{
				card_type = WxCardType.GENERAL_COUPON,
				general_coupon = cardReq
			};
			return await this.AddCardAsync(cardReq2);
		}

		/// <summary>
		///   添加折扣卡券接口
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		public async Task<WxAddCardResp> AddDiscountCardAsync(WxDiscountCardMo cardReq)
		{
			var cardReq2 = new
			{
				card_type = WxCardType.DISCOUNT,
				discount = cardReq
			};
			return await this.AddCardAsync(cardReq2);
		}

		/// <summary>
		///   添加礼品卡券接口
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		public async Task<WxAddCardResp> AddGiftCardAsync(WxGiftCardMo cardReq)
		{
			var cardReq2 = new
			{
				card_type = WxCardType.GIFT,
				gift = cardReq
			};
			return await this.AddCardAsync(cardReq2);
		}

		/// <summary>
		///   添加团购卡券接口
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		public async Task<WxAddCardResp> AddGrouponCardAsync(WxGroupCardMo cardReq)
		{
			var cardReq2 = new
			{
				card_type = WxCardType.GROUPON,
				groupon = cardReq
			};
			return await this.AddCardAsync(cardReq2);
		}

		/// <summary>
		///   添加团会员卡券接口
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		public async Task<WxAddCardResp> AddMemberCardAsync(WxMemberCardMo cardReq)
		{
			var cardReq2 = new
			{
				card_type = WxCardType.MEMBER_CARD,
				member_card = cardReq
			};
			return await this.AddCardAsync(cardReq2);
		}

		/// <summary>
		///   添加朋友的卡券接口
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		public async Task<WxAddCardResp> AddFriendCard(WxFriendCardBigMo cardReq)
		{
			return await this.AddCardAsync(cardReq);
		}

		/// <summary>
		///   添加会议门票卡券接口
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		public async Task<WxAddCardResp> AddMeetingCardAsync(WxMeetingCardMo cardReq)
		{
			var cardReq2 = new
			{
				card_type = WxCardType.MEETING_TICKET,
				meeting_ticket = cardReq
			};
			return await this.AddCardAsync(cardReq2);
		}

		/// <summary>
		///   添加 景点门票 卡券接口
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		public async Task<WxAddCardResp> AddScenicCardAsync(WxScenicCardMo cardReq)
		{
			var cardReq2 = new
			{
				card_type = WxCardType.SCENIC_TICKET,
				scenic_ticket = cardReq
			};
			return await this.AddCardAsync(cardReq2);
		}

		/// <summary>
		///   添加 电影票 卡券接口
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		public async Task<WxAddCardResp> AddMovieCardAsync(WxMovieCardMo cardReq)
		{
			var cardReq2 = new
			{
				card_type = WxCardType.MOVIE_TICKET,
				movie_ticket = cardReq
			};
			return await this.AddCardAsync(cardReq2);
		}

		/// <summary>
		///   添加 飞机票 卡券接口
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		public async Task<WxAddCardResp> AddBoardCardAsync(WxBoardCardMo cardReq)
		{
			var cardReq2 = new
			{
				card_type = WxCardType.BOARDING_PASS,
				boarding_pass = cardReq
			};
			return await this.AddCardAsync(cardReq2);
		}

		/// <summary>
		///   添加卡券
		/// </summary>
		/// <param name="cardReq"></param>
		/// <returns></returns>
		private async Task<WxAddCardResp> AddCardAsync(object cardReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/create";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				card = cardReq
			}, Formatting.Indented, new JsonSerializerSettings
			{
				NullValueHandling = NullValueHandling.Ignore
			});
			return await base.RestCommonOffcialAsync<WxAddCardResp>(LHttpRequest, null);
		}

		/// <summary>
		///  获取用户的卡券code列表
		/// </summary>
		/// <param name="openId">需要查询的用户openid</param>
		/// <param name="cardId">卡券ID。不填写时默认查询当前appid下的卡券</param>
		/// <returns></returns>
		public async Task<WxGetUserCardCodeListResp> GetUserCardCodeListAsync(string openId, string cardId)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/user/getcardlist";
			LHttpRequest.CustomBody = string.Format("{{\"openid\":\"{0}\",\"card_id\":\"{1}\"}}", openId, cardId);
			return await base.RestCommonOffcialAsync<WxGetUserCardCodeListResp>(LHttpRequest, null);
		}

		/// <summary>
		///   获取卡券详情
		/// </summary>
		/// <param name="cardId"></param>
		/// <returns></returns>
		public async Task<WxGetCardDetailRsp> GetCardDetailAsync(string cardId)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/get";
			LHttpRequest.CustomBody = string.Format("{{\"card_id\":\"{0}\"}}", cardId);
			return await base.RestCommonOffcialAsync<WxGetCardDetailRsp>(LHttpRequest, null);
		}

		/// <summary>
		///   获取卡券id列表
		/// </summary>
		/// <param name="offset">查询卡列表的起始偏移量，从0开始，即offset: 5是指从从列表里的第六个开始读取</param>
		/// <param name="count"> 需要查询的卡片的数量（数量最大50</param>
		/// <param name="status"> 可空 支持开发者拉出指定状态的卡券列表</param>
		/// <returns></returns>
		public async Task<WxGetCardIdListResp> GetCardIdListAsync(int offset, int count, List<string> status)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/batchget";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				offset = offset,
				count = count,
				status_list = status
			});
			return await base.RestCommonOffcialAsync<WxGetCardIdListResp>(LHttpRequest, null);
		}

		/// <summary>
		///   修改现金卡券接口
		/// </summary>
		/// <param name="cardId">卡券Id</param>
		/// <param name="cardMo">修改的基础信息</param>
		/// <returns></returns>
		public async Task<WxUpdateCardResp> UpdateCashCardAsync(string cardId, WxUpdateCardBaseReq cardMo)
		{
			var obj = new
			{
				card_id = cardId,
				cash = cardMo
			};
			return await this.UpdateCardAsync(obj);
		}

		/// <summary>
		///   修改 优惠卡 券接口
		/// </summary>
		/// <param name="cardId">卡券Id</param>
		/// <param name="cardMo">修改的基础信息</param>
		/// <returns></returns>
		public async Task<WxUpdateCardResp> UpdateCoupnCardAsync(string cardId, WxUpdateCardBaseReq cardMo)
		{
			var obj = new
			{
				card_id = cardId,
				general_coupon = cardMo
			};
			return await this.UpdateCardAsync(obj);
		}

		/// <summary>
		///  修改折扣卡券接口
		/// </summary>  
		/// <param name="cardId">卡券Id</param>
		/// <param name="cardMo">修改对应的卡券相关信息</param>
		/// <returns></returns>
		public async Task<WxUpdateCardResp> UpdateDiscountCardAsync(string cardId, WxUpdateCardBaseReq cardMo)
		{
			var obj = new
			{
				card_id = cardId,
				discount = cardMo
			};
			return await this.UpdateCardAsync(obj);
		}

		/// <summary>
		///   修改礼品卡券接口
		/// </summary>
		/// <param name="cardId">卡券Id</param>
		/// <param name="cardMo">修改对应的卡券相关信息</param>
		/// <returns></returns>
		public async Task<WxUpdateCardResp> UpdateGiftCardAsync(string cardId, WxUpdateCardBaseReq cardMo)
		{
			var obj = new
			{
				card_id = cardId,
				gift = cardMo
			};
			return await this.UpdateCardAsync(obj);
		}

		/// <summary>
		///   添加团购卡券接口
		/// </summary>
		/// <param name="cardId">卡券Id</param>
		/// <param name="cardMo">修改对应的卡券相关信息</param>
		/// <returns></returns>
		public async Task<WxUpdateCardResp> UpdateGrouponCardAsync(string cardId, WxUpdateCardBaseReq cardMo)
		{
			var obj = new
			{
				card_id = cardId,
				groupon = cardMo
			};
			return await this.UpdateCardAsync(obj);
		}

		/// <summary>
		///   修改 会员卡券 接口
		/// </summary>
		/// <param name="cardId">卡券Id</param>
		/// <param name="cardMo">修改对应的卡券相关信息</param>
		/// <returns></returns>
		public async Task<WxUpdateCardResp> UpdateMemberCardAsync(string cardId, WxUpdateMemberCardReq cardMo)
		{
			var obj = new
			{
				card_id = cardId,
				member_card = cardMo
			};
			return await this.UpdateCardAsync(obj);
		}

		/// <summary>
		///   修改 会议门票卡券接口
		/// </summary>
		/// <param name="cardId">卡券Id</param>
		/// <param name="cardMo">修改对应的卡券相关信息</param>
		/// <returns></returns>
		public async Task<WxUpdateCardResp> UpdateMeetingCardAsync(string cardId, WxUpdateMeetingCardReq cardMo)
		{
			var obj = new
			{
				card_id = cardId,
				meeting_ticket = cardMo
			};
			return await this.UpdateCardAsync(obj);
		}

		/// <summary>
		///   修改 景点门票 卡券接口
		/// </summary>
		/// <param name="cardId">卡券Id</param>
		/// <param name="cardMo">修改对应的卡券相关信息</param>
		/// <returns></returns>
		public async Task<WxUpdateCardResp> UpdateScenicCardAsync(string cardId, WxScenicCardMo cardMo)
		{
			var obj = new
			{
				card_id = cardId,
				scenic_ticket = cardMo
			};
			return await this.UpdateCardAsync(obj);
		}

		/// <summary>
		///   修改 电影票 卡券接口
		/// </summary>
		/// <param name="cardId">卡券Id</param>
		/// <param name="cardMo">修改对应的卡券相关信息</param>
		/// <returns></returns>
		public async Task<WxUpdateCardResp> UpdateMovieCardAsync(string cardId, WxUpdateMovieCardReq cardMo)
		{
			var obj = new
			{
				card_id = cardId,
				movie_ticket = cardMo
			};
			return await this.UpdateCardAsync(obj);
		}

		/// <summary>
		///   修改 飞机票 卡券接口
		/// </summary>
		/// <param name="cardId">卡券Id</param>
		/// <param name="cardMo">修改对应的卡券相关信息</param>
		/// <returns></returns>
		public async Task<WxUpdateCardResp> UpdateBoardCardAsync(string cardId, WxUpdateBoardCardReq cardMo)
		{
			var obj = new
			{
				card_id = cardId,
				boarding_pass = cardMo
			};
			return await this.UpdateCardAsync(obj);
		}

		/// <summary>
		///  修改卡券信息
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		private async Task<WxUpdateCardResp> UpdateCardAsync(object obj)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/update";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings
			{
				NullValueHandling = NullValueHandling.Ignore
			});
			return await base.RestCommonOffcialAsync<WxUpdateCardResp>(LHttpRequest, null);
		}

		/// <summary>
		///  更新库存
		/// </summary>
		/// <param name="cardId">卡Id</param>
		/// <param name="increaseCount">增加的数量</param>
		/// <param name="reduceCount">减少的数量</param>
		/// <returns></returns>
		public async Task<WxBaseResp> UpdateStockAsync(string cardId, int increaseCount, int reduceCount)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/modifystock";
			LHttpRequest.CustomBody = string.Format("{{\"card_id\":\"{0}\",\"increase_stock_value\":{1},\"reduce_stock_value\":{2}}}", cardId, increaseCount, reduceCount);
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		///  修改卡券code
		/// </summary>
		/// <param name="code">code</param>
		/// <param name="cardId">卡Id</param>
		/// <param name="newCode">新code</param>
		/// <returns></returns>
		public async Task<WxBaseResp> UpdateCardCodeAsync(string code, string cardId, string newCode)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/code/update";
			LHttpRequest.CustomBody = string.Format("{{\"card_id\":\"{0}\",\"code\":\"{1}\",\"new_code\":\"{2}\"}}", cardId, code, newCode);
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		///  删除卡券
		/// </summary>
		/// <param name="cardId">卡Id</param>
		/// <returns></returns>
		public async Task<WxBaseResp> DeleteCardAsync(string cardId)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/delete";
			LHttpRequest.CustomBody = string.Format("{{\"card_id\":\"{0}\"}}", cardId);
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 废弃指定code卡券
		/// </summary>
		/// <param name="code"></param>
		/// <param name="cardId">卡Id，自定义code时 必填</param>
		/// <param name="reason">废弃理由，自定义code时 可空</param>
		/// <returns></returns>
		public async Task<WxBaseResp> AbandonCardCodeAsync(string code, string cardId, string reason)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/code/unavailable";
			LHttpRequest.CustomBody = string.Format("{{\"code\":\"{0}\",\"card_id\":\"{1}\",\"reason\":\"{2}\"}}", code, cardId, reason);
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 6.1  接口激活会员卡
		/// </summary>
		/// <param name="activeReq"></param>
		/// <returns></returns>
		public async Task<WxBaseResp> ActiveMemberCardAsync(WxActiveMemberCardReq activeReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/card/membercard/activate";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(activeReq);
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 设置激活开卡表单
		/// </summary>
		/// <param name="setReq"></param>
		/// <returns></returns>
		public async Task<WxBaseResp> SetActiveFormAsync(WxSetActiveFormReq setReq)
		{
			LHttpRequest req = new LHttpRequest
			{
				HttpMothed = HttpMothed.Post,
				AddressUrl = "https://api.weixin.qq.com" + "/card/membercard/activateuserform/set",
				CustomBody = JsonConvert.SerializeObject(setReq)
			};
			return await base.RestCommonOffcialAsync<WxBaseResp>(req, null);
		}

		/// <summary>
		/// 根据CardID和Code查询会员信息。
		/// </summary>
		/// <param name="code"></param>
		/// <param name="cardId"></param>
		/// <returns></returns>
		public async Task<WxGetMemberCardUserInfoResp> GetMemberCardUserInfoAsync(string code, string cardId)
		{
			LHttpRequest req = new LHttpRequest
			{
				HttpMothed = HttpMothed.Post,
				AddressUrl = "https://api.weixin.qq.com" + "/card/membercard/userinfo/get",
				CustomBody = string.Format("{{\"card_id\":\"{0}\",\"code\":\"{1}\"}}", cardId, code)
			};
			return await base.RestCommonOffcialAsync<WxGetMemberCardUserInfoResp>(req, null);
		}

		/// <summary>
		/// 根据CardID和Code查询会员信息。
		/// </summary>
		/// <param name="activateTicket">临时票据</param>
		/// <returns></returns>
		public async Task<WxGetActiveTempInfoResp> GetMemberActiveTempInfoAsync(string activateTicket)
		{
			LHttpRequest req = new LHttpRequest
			{
				HttpMothed = HttpMothed.Post,
				AddressUrl = "https://api.weixin.qq.com" + "/card/membercard/userinfo/get",
				CustomBody = string.Format("{{\"activate_ticket\":\"{0}\"}}", activateTicket)
			};
			return await base.RestCommonOffcialAsync<WxGetActiveTempInfoResp>(req, null);
		}

		/// <summary>
		/// 当会员持卡消费后，开发者调用该接口更新会员信息。
		/// </summary>
		/// <param name="updateReq">更新信息</param>
		/// <returns></returns>
		public async Task<WxUpdateMemberCardUserInfoResp> UpdateMemberCardUserInfoAsync(WxUpdateMemberCardUserInfoReq updateReq)
		{
			LHttpRequest req = new LHttpRequest
			{
				HttpMothed = HttpMothed.Post,
				AddressUrl = "https://api.weixin.qq.com" + "/card/membercard/updateuser",
				CustomBody = JsonConvert.SerializeObject(updateReq)
			};
			return await base.RestCommonOffcialAsync<WxUpdateMemberCardUserInfoResp>(req, null);
		}

		/// <summary>
		/// 拉取本商户的总体数据情况，包括时间区间内的各指标总量。
		/// 查询时间区间需 小于 62 天
		/// </summary>
		/// <param name="beginDate">查询数据的起始时间</param>
		/// <param name="endTime">查询数据的截至时间</param>
		/// <param name="source">卡券来源，0为公众平台创建的卡券数据、1是API创建的卡券数据</param>
		/// <returns></returns>
		public async Task<WxCardStatResp<WxCardStatMo>> GetCardStatisticAsync(DateTime beginDate, DateTime endTime, int source)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getcardbizuininfo";
			LHttpRequest.CustomBody = string.Format("{{\"begin_date\":\"{0:yyyy-MM-dd}\",\"end_date\":\"{1:yyyy-MM-dd}\",\"cond_source\":{2}}}", beginDate, endTime, source);
			return await base.RestCommonOffcialAsync<WxCardStatResp<WxCardStatMo>>(LHttpRequest, null);
		}

		/// <summary>
		/// 开发者调用该接口拉取减免卡券（优惠券、团购券、折扣券、礼品券）在固定时间区间内的相关数据
		/// </summary>
		/// <param name="beginDate">查询数据的起始时间</param>
		/// <param name="endTime">查询数据的截至时间</param>
		/// <param name="source">卡券来源，0为公众平台创建的卡券数据、1是API创建的卡券数据</param>
		/// <param name="cardId">可空，卡券ID。填写后，指定拉出该卡券的相关数据</param>
		/// <returns></returns>
		public async Task<WxCardStatResp<WxCardItemStatMo>> GetCardItemStatisticAsync(DateTime beginDate, DateTime endTime, int source, string cardId)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getcardcardinfo";
			LHttpRequest.CustomBody = string.Format("{{\"begin_date\":\"{0:yyyy-MM-dd}\",\"end_date\":\"{1:yyyy-MM-dd}\",\"cond_source\":{2},\"card_id\":\"{3}\"}}", beginDate, endTime, source, cardId);
			return await base.RestCommonOffcialAsync<WxCardStatResp<WxCardItemStatMo>>(LHttpRequest, null);
		}

		/// <summary>
		/// 拉取公众平台创建的会员卡相关数据
		/// </summary>
		/// <param name="beginDate">查询数据的起始时间</param>
		/// <param name="endTime">查询数据的截至时间</param>
		/// <param name="source">卡券来源，0为公众平台创建的卡券数据、1是API创建的卡券数据</param>
		/// <returns></returns>
		public async Task<WxCardStatResp<WxMemberCardStatMo>> GetMemberCardStatisticAsync(DateTime beginDate, DateTime endTime, int source)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getcardmembercardinfo";
			LHttpRequest.CustomBody = string.Format("{{\"begin_date\":\"{0:yyyy-MM-dd}\",\"end_date\":\"{1:yyyy-MM-dd}\",\"cond_source\":{2}}}", beginDate, endTime, source);
			return await base.RestCommonOffcialAsync<WxCardStatResp<WxMemberCardStatMo>>(LHttpRequest, null);
		}

		/// <summary>
		/// 开发者调用该接口拉取减免卡券（优惠券、团购券、折扣券、礼品券）在固定时间区间内的相关数据
		/// </summary>
		/// <param name="beginDate">查询数据的起始时间</param>
		/// <param name="endTime">查询数据的截至时间</param>
		/// <param name="cardId">卡券ID</param>
		/// <returns></returns>
		public async Task<WxCardStatResp<WxMemberCardDetailStatMo>> GetMemberCardDetailStatisticAsync(DateTime beginDate, DateTime endTime, string cardId)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getcardmembercarddetail";
			LHttpRequest.CustomBody = string.Format("{{\"begin_date\":\"{0:yyyy-MM-dd}\",\"end_date\":\"{1:yyyy-MM-dd}\",\"card_id\":\"{2}\"}}", beginDate, endTime, cardId);
			return await base.RestCommonOffcialAsync<WxCardStatResp<WxMemberCardDetailStatMo>>(LHttpRequest, null);
		}
	}
}
